package utils

import (
	"fmt"
	"reflect"
	"regexp"
	"strconv"
	"strings"
)

// String2Int32 convert string to int32
func String2Int32(s string) (int32, error) {
	i64, err := strconv.ParseInt(s, 10, 32)
	if err != nil {
		return 0, err
	}
	return int32(i64), nil
}

func ReverseString(s []string) []string {
	for i := 0; i < len(s)/2; i++ {
		j := len(s) - i - 1
		s[i], s[j] = s[j], s[i]
	}
	return s
}

// func NoInArrEleMent(arr []string, ele []string) []string {
// 	var res []string
// 	for _, v := range ele {
// 		found := false
// 		for _, v1 := range arr {
// 			if v == v1 {
// 				found = true
// 				break
// 			}
// 		}
// 		if !found {
// 			res = append(res, v)
// 		}
// 	}
// 	return res
// }

// generic NoInArrEleMent
func NoInArrEleMent[T comparable](arr []T, ele []T) []T {
	var res []T
	for _, v := range ele {
		found := false
		for _, v1 := range arr {
			if v == v1 {
				found = true
				break
			}
		}
		if !found {
			res = append(res, v)
		}
	}
	return res
}

// string to int64
func String2Int64(s string) (int64, error) {
	i64, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		return 0, err
	}
	return i64, nil
}

func UniqueEleMent[T comparable](arr []T) []T {
	// create a map to store the frequency of each element
	exist := make(map[T]bool)
	// create a slice to store the unique elements
	unique := make([]T, 0, len(exist))

	// add the unique elements to the slice
	for _, val := range arr {
		if !exist[val] {
			unique = append(unique, val)
			exist[val] = true
		}
	}

	return unique
}

func IsNoInArrEleMent[T comparable](arr []T, val T) bool {
	for _, elem := range arr {
		if elem == val {
			return false
		}
	}
	return true
}

// int642String convert int64 to string
func Int642String(i int64) string {
	return strconv.FormatInt(i, 10)
}

func StringArrToString(s []string) string {
	myString := strings.Join(s, `","`)
	myString = `"` + myString + `"`
	return myString
}

func ObjectAssignBySameTypeName(in, out interface{}) interface{} {
	inOf := reflect.TypeOf(in)
	outOf := reflect.TypeOf(out)
	inElem := reflect.ValueOf(inOf).Elem()
	outElem := reflect.ValueOf(outOf).Elem()
	for i := 0; i < inOf.NumField(); i++ {
		for j := 0; j < outOf.NumField(); j++ {
			if inOf.Field(i).Type != nil && inOf.Field(i).Name == outOf.Field(j).Name {
				outElem.Field(j).Set(inElem.Field(i))
			}
		}
	}
	return out
}

// RemoveDuplicateStringSlice 数组去重 string
func RemoveDuplicateStringSlice(languages []string) []string {
	result := make([]string, 0, len(languages))
	temp := map[string]struct{}{}
	for _, item := range languages {
		if _, ok := temp[item]; !ok {
			temp[item] = struct{}{}
			result = append(result, item)
		}
	}
	return result
}

// RemoveDuplicateInt64Slice 数组去重 int64
func RemoveDuplicateInt64Slice(languages []int64) []int64 {
	result := make([]int64, 0, len(languages))
	temp := map[int64]struct{}{}
	for _, item := range languages {
		if _, ok := temp[item]; !ok {
			temp[item] = struct{}{}
			result = append(result, item)
		}
	}
	return result
}

/**
 * @Author Chenlin
 * @Param
 * @return
 * @Description $$符转HTML $$c = \pm\sqrt{a^2 + b^2}$$ <span data-w-e-type="formula" data-w-e-is-void data-w-e-is-inline data-value="c = \pm\sqrt{a^2 + b^2}"></span>
 * 雨哥
 **/

func ConvertDollarLatexToHTML(text string) string {
	outerCompile := regexp.MustCompile(`\$\$.*?\$\$`)
	submatch := outerCompile.FindAllStringSubmatch(text, -1)
	fmt.Println(submatch)
	out := make([]string, len(submatch))
	resHtml := text
	for i := range out {
		//innerCompile := regexp.MustCompile(`(\?<=\$\$).*?(\?=\$\$)`)
		//latex := innerCompile.FindAllStringSubmatch(submatch[i][0], -1)
		//fmt.Println(latex)
		newStr := strings.Replace(submatch[i][0], "$$", "", -1)
		fmt.Println(newStr)
		latexHtml := fmt.Sprintf("<span data-w-e-type=\"formula\" data-w-e-is-void data-w-e-is-inline data-value=\"%s\"></span>", newStr)
		resHtml = strings.Replace(resHtml, submatch[i][0], latexHtml, -1)
		out[i] = submatch[i][0]
	}
	return resHtml
}

/**
* @Author Chenlin
* @Param
* @return
* @Description HTML转$$符,将 "<span data-w-e-type="formula" data-w-e-is-void data-w-e-is-inline data-value="c = \pm\sqrt{a^2 + b^2}"></span>" 转为  "$$c = \pm\sqrt{a^2 + b^2}$$"
* 沈燕南
**/

func ConvertHTMLTODollarLatex(text string) string {
	outerCompile := regexp.MustCompile(`\<span data-w-e-type="formula" data-w-e-is-void data-w-e-is-inline.*?\</span>`)
	submatch := outerCompile.FindAllStringSubmatch(text, -1)
	fmt.Println(submatch)
	out := make([]string, len(submatch))
	resHtml := text
	for i := range out {
		innerCompile := regexp.MustCompile(`<span[^>]+\bdata-value=["']([^"']+)["']`)
		latex := innerCompile.FindAllStringSubmatch(submatch[i][0], -1)
		latexDollar := fmt.Sprintf("$$%s$$", latex[0][1])
		resHtml = strings.Replace(resHtml, submatch[i][0], latexDollar, -1)
		out[i] = submatch[i][0]
	}
	return resHtml
}

/**
 * @Author Chenlin
 * @Param
 * @return
 * @Description $$符去除 将 "$$c = \pm\sqrt{a^2 + b^2}$$" 转为 "c = \pm\sqrt{a^2 + b^2}"
 **/

func ConvertDollarLatexToLaTeX(text string) string {
	outerCompile := regexp.MustCompile(`\$\$.*?\$\$`)
	submatch := outerCompile.FindAllStringSubmatch(text, -1)
	fmt.Println(submatch)
	out := make([]string, len(submatch))
	resHtml := text
	for i := range out {
		newStr := strings.Replace(submatch[i][0], "$$", "", -1)
		fmt.Println(newStr)
		resHtml = strings.Replace(resHtml, submatch[i][0], newStr, -1)
		out[i] = submatch[i][0]
	}
	return resHtml
}
